package com.woniuxy.house.mapper;

import com.woniuxy.house.entity.Post;
import com.woniuxy.house.provider.PostSqlProvider;
import javafx.geometry.Pos;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.lang.reflect.Type;
import java.util.List;

/**
 * 帖子mapper接口类
 */
public interface PostMapper {
    /**
     * 获取求租所有的帖子
     * 默认sign为q时 帖子为求租贴
     */

    @Select("select * from house_post where isacross='y' and sign='q'")
    @Results(id = "result", value = {
            @Result(column = "id", property = "id", id = true),

            @Result(column = "time", property = "time"),
            @Result(column = "title", property = "title"),
            @Result(column = "sign", property = "sign"),
            @Result(column = "isacross", property = "isAcross"),
            @Result(column = "user_id", property = "user",one = @One(select = "com.woniuxy.house.mapper.UserMapper.getById",fetchType = FetchType.LAZY)),
            @Result(column = "isdelete", property = "isDelete"),
            @Result(column = "longitude",property = "longitude"),
            @Result(column = "latitude",property = "latitude"),
            @Result(column = "post_detail_id", property = "postDetail", one = @One(select = "com.woniuxy.house.mapper.PostDetailMapper.getById", fetchType = FetchType.LAZY)),
            @Result(column = "id", property = "postPictures", many = @Many(select = "com.woniuxy.house.mapper.PostPictureMapper.getByPostId", fetchType = FetchType.LAZY))
    })
    List<Post> getQiuZuAll();

    /**
     * 获取求租所有的帖子
     * 默认sign为z时 帖子为找租贴
     */

    @Select("select * from house_post where isacross='y' and sign='z'")
    @ResultMap(value = "result")
    List<Post> getZhaoZuAll();

    /**
     * 根据帖子id 找到帖子
     */
    @Select("select * from house_post where id=#{id}")
    @ResultMap(value = "result")
    Post getById(Integer id);
    /**
     * 获取所有帖子
     */
    @Select("select * from house_post  ")
    @ResultMap(value = "result")
    List<Post> getAll();

    /**
     * 获取所有的帖子 （按照时间降序）
     */
    @Select("select * from house_post order by time desc ")
    @ResultMap(value = "result")
    List<Post> getAllByDesc();

    /**
     * 根据用户ID获取帖子
	 * 20220905修改添加查询条件，isdelete='n'
     */


    @Select("select * from house_post where isacross='y' and user_id=#{id} and isdelete='n'")
    @ResultMap(value = "result")
    List<Post> getByUserId(Integer userId);

    /**
     * 增加帖子
     */


    @Insert("insert into house_post(time,title,sign,isacross,isdelete,user_id,post_detail_id,longitude,latitude) value(#{time},#{title},#{sign},#{isAcross},#{isDelete},#{user.id},#{postDetail.id},#{longitude},#{latitude})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void addPost(Post post);


    /**
     * 根据帖子ID进行条件修改（修改内容仅为title（用户自己修改标题内容），
     * isacross（后台管理时审核是否通过），isdelete（逻辑删除））
     */

    @UpdateProvider(type = PostSqlProvider.class, method = "update")
    void updateByCondition(Post post);

    /**
     * 模糊搜索
     */
    //@Select("select * from house_post where title like concat('%',#{title},'%')")
    @SelectProvider(type = PostSqlProvider.class, method = "getByCondition")
    @ResultMap(value = "result")
    List<Post> getBySearchText(Post post);

	@Select("select * from house_post where user_id=#{uid} and id=#{pid}")
	@ResultMap("result")
	Post getByUidAndPid(@Param("uid")Integer uid,@Param("pid") Integer pid);

@Select("select*from house_user_post hu, house_post hp where hu.user_id=#{userId} and hu.post_id=hp.id")
    @ResultMap(value = "result")
    List<Post> getByUserIdPost(Integer userId);
}
